﻿using Bll;
using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Myproject.BSM.Ashx.PcChannel
{
    /// <summary>
    /// ArticleEdit 的摘要说明
    /// </summary>
    public class ArticleEdit : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string id = context.Request.Form["pc_Channel_ArticleEdit_WebArticleId"];
            string name = context.Request.Form["pc_Channel_ArticleEdit_name"];
            string ename = context.Request.Form["pc_Channel_ArticleEdit_ename"];
            string url = context.Request.Form["pc_Channel_ArticleEdit_url"];
            string pid = context.Request.Form["pc_Channel_ArticleEdit_parentID"];
            if (id == "0")
            {
                ArticleModel model = new ArticleModel();
                model.ChanneTitle = name;
                model.EChanneTitle = ename;
                model.ParentId = int.Parse(pid);
                if (string.IsNullOrEmpty(url))
                {
                    model.IsUrl = false;
                }
                else
                {
                    model.IsUrl = true;
                    model.Url = url;
                }
                model.AddAdminId = int.Parse(cookieHelp.AdminId);
                model.UpdateAdminId = int.Parse(cookieHelp.AdminId);
                model.IsEnd = true;
                if (new ArticleBLL().Add(model) > 0)
                {
                    //修改父频道结束标示[新增子频道，父频道isend=false]
                    ArticleModel model2 = new ArticleBLL().GetModel(int.Parse(pid));
                    if (model2 != null)
                    {
                        model2.IsEnd = false;
                        new ArticleBLL().Update(model2);
                    }

                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "添加频道成功！", "pc_channel_ArticleList", "", "", true, "", ""));
                }
                else
                {
                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "添加频道失败！", "pc_channel_ArticleList", "", "", true, "", ""));
                }
            }
            else
            {
                ArticleModel model = new ArticleBLL().GetModel(int.Parse(id));
                model.ChanneTitle = name;
                model.EChanneTitle = ename;
                if (string.IsNullOrEmpty(url))
                {
                    model.IsUrl = false;
                }
                else
                {
                    model.IsUrl = true;
                    model.Url = url;
                }
                model.UpdateAdminId = int.Parse(cookieHelp.AdminId);
                if (new ArticleBLL().Update(model))
                {
                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改频道成功！", "pc_channel_ArticleList", "", "", true, "", ""));
                }
                else
                {
                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改频道失败！", "pc_channel_ArticleList", "", "", true, "", ""));
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}